package com.xzq.erp.controller;

import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.constants.JwtConstants;
import com.xzq.erp.domain.dto.LoginDTO;
import com.xzq.erp.domain.dto.RegisterDTO;
import com.xzq.erp.domain.vo.LoginVO;
import com.xzq.erp.domain.vo.WxRefreshVO;
import com.xzq.erp.service.IAuthService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Tag(name = "认证相关接口")
public class AuthController {

    private final IAuthService authService;

    /**
     * PC端用户登录
     */
    @PostMapping(value = "/login")
    @Operation(summary = "用户登录")
    @ApiOperationLog(description = "用户登录")
    public Response<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        return Response.success(authService.login(loginDTO));
    }

    /**
     * 移动端用户登录
     */
    @PostMapping(value = "/wx/login")
    @Operation(summary = "微信用户登录")
    @ApiOperationLog(description = "微信用户登录")
    public Response<LoginVO> WxLogin(@Valid @RequestBody LoginDTO loginDTO) {
        return Response.success(authService.WxLogin(loginDTO));
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    @ApiOperationLog(description = "用户注册")
    public Response<Integer> register(@Valid @RequestBody RegisterDTO registerDTO) {
        return Response.success(authService.register(registerDTO));
    }

    /**
     * 刷新令牌
     */
    @PostMapping("/refreshToken")
    @Operation(summary = "刷新令牌")
    @ApiOperationLog(description = "刷新令牌")
    public Response<String> refreshToken(@CookieValue(name = JwtConstants.REFRESH_HEADER) String refreshToken) {
        return Response.success(authService.refreshToken(refreshToken));
    }

    /**
     * 微信用户刷新令牌
     */
    @PostMapping("/wx/refreshToken")
    @Operation(summary = "刷新令牌")
    @ApiOperationLog(description = "刷新令牌")
    public Response<WxRefreshVO> wxRefreshToken(@RequestHeader(name = JwtConstants.REFRESH_HEADER) String refreshToken) {
        return Response.success(authService.wxRefreshToken(refreshToken));
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    @Operation(summary = "登出")
    @ApiOperationLog(description = "登出")
    public Response<Void> logout(HttpServletResponse response) {
        authService.logout(response);
        return Response.success();
    }
} 